﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Services.Gateway.Runtime;
using InvokeWebService.Messages;
using PServiceBus.Transports;
using System.Diagnostics;
using PServiceBus.Core.Runtime.Transports;

namespace InvokeWebService {
    class Program {
        static object _handler = null;
        static void Main(string[] args) {

            //Register Topic just incase
                Topic.RegisterIfNotExist<WebServiceInvokeCompleted>();

            var subscriber = Subscriber.New("WebServiceReceiver")
                .SubscribeTo(Topic.Select<WebServiceInvokeCompleted>().NeedHeaders(true))
                .AddTransportFor<WebServiceInvokeCompleted>("Tcp", Transport.New<TcpTransport>(
                    transport => {
                        transport.IPAddress = "127.0.0.1";
                        transport.Port = 11011;
                        transport.Format = PServiceBus.Core.Runtime.Transports.TransportFormat.Xml;
                    }));

            subscriber.Save();

            _handler = subscriber.OnMessageReceived<WebServiceInvokeCompleted>("Tcp",
                msg => {
                    Console.WriteLine("WebService Response: {0}", msg.Message);
                    if (msg.Headers != null)
                        Console.WriteLine("WebService Response Headers:\n {0}",
                           String.Join("\r\n", msg.Headers.ToList().Select(
                           header => String.Format("{0}: {1}", header.Key, header.Value))));
                    else
                        Console.WriteLine("Could not read message header");
                },
                error => {
                    error.Continue = true;
                    Console.WriteLine("Error: {0}", error.Error.ToString());
                });


            
            Console.WriteLine("Waiting for requests.. Press enter to send requests. Type exit to close terminal");

            while (true) {
                var text = Console.ReadLine().ToLower();
                if (text == "exit") break;
                PublishMessage();
            }
            subscriber.Delete();
            TransportHandlers.ShutDown();
        }
        static void PublishMessage() {
            ESB.With<WebServiceTransport>(
                transport =>
                {
                    transport.Format = TransportFormat.Xml;
                    transport.Method = "HelloWorld";
                    transport.Namespace = "http://tempuri.org";
                    transport.Url = "http://localhost:55196/WebService1.asmx";
                    transport.MethodParameters = new Dictionary<string, string>() { { "data", "To" } };
                }).Send(new
                {
                    To = new
                    {
                        Text = "Olamide",
                        List = new { Data = new List<object>() { new { Text = "Olamide" } } },
                        Parent = new { Text = "Parent" }
                    }
                });

            ESB.With<HttpTransport>(
                transport =>
                {
                    transport.Format = TransportFormat.Xml;
                    transport.QueryParams = new Dictionary<string, string>() { { "to", "To.Value" } };
                    transport.Url = "http://localhost:55196/WebForm1.aspx";
                }).Send(new { To = new { Value = "Olamide" } });
        }
    }



    public class Test {
        public string Text { get; set; }
    }
}
 